package castledefense.gui;

import java.util.ArrayList;

import castledefense.data.GameData;
import castledefense.gui.upgradePanel.UpgradePanelDrawing;
import castledefense.logic.Selectable;
import castledefense.logic.buildings.Building;

import de.matthiasmann.twl.GUI;

import twl.global.widget.GlobalPanel;

public class UpgradePanel extends GlobalPanel {

	private GUIMainGame stateGui;

	private UpgradeButton fireUp = new UpgradeButton("fire", GameData.fire, this);
	private UpgradeButton waterUp = new UpgradeButton("water", GameData.water, this);
	private UpgradeButton earthUp = new UpgradeButton("earth", GameData.earth, this);
	private UpgradeButton airUp = new UpgradeButton("air", GameData.air, this);

	private ArrayList<UpgradeButton> upgradeButtonList = new ArrayList<UpgradeButton>();

	private Selectable whichInfo = null;
	
	private UpgradePanelDrawing panelDrawing;


	public UpgradePanel(GUIMainGame gui) {
		this.stateGui = gui;

		
		setTheme("panelTheme");
		// Add building button
		add(fireUp);
		upgradeButtonList.add(fireUp);
		fireUp.setPosition(10, 190);
		add(waterUp);
		upgradeButtonList.add(waterUp);
		waterUp.setPosition(50, 190);

		add(earthUp);
		upgradeButtonList.add(earthUp);
		earthUp.setPosition(90, 190);

		add(airUp);
		upgradeButtonList.add(airUp);
		airUp.setPosition(130, 190);
		
		setVisible(false);
		
		panelDrawing = new UpgradePanelDrawing(this);

	}

	public GUIMainGame getStateGUI() {
		return stateGui;
	}

	@Override
	protected void paintWidget(GUI gui){
		panelDrawing.drawInfo(whichInfo);
	}

	public void setWhichInfo(Selectable tmp) {
		whichInfo = tmp;
		setVisible(true);
		if (whichInfo != null) {
			if (whichInfo instanceof Building) {
				for (UpgradeButton index : upgradeButtonList) {
					index.setVisible(true);
				}
			} else {
				for (UpgradeButton index : upgradeButtonList) {
					index.setVisible(false);
				}
			}
		}
	}

	public void nullWhichInfo() {
		if (whichInfo instanceof Building) {
			for (UpgradeButton index : upgradeButtonList) {
				index.setVisible(false);
			}
		}
		whichInfo = null;
		setVisible(false);
	}

	public void nullWhichInfoIfSelected(Selectable tmp) {
		if (tmp != null && whichInfo != null) {
			if (whichInfo.equals(tmp)) {
				nullWhichInfo();
			}
		}
	}

	public boolean isInfo() {
		return (whichInfo != null);
	}

	public Selectable getSelected() {
		return whichInfo;
	}

}
